package com.ydlclass.ChannelHandler.handler.InboundHandler;

import com.ydlclass.Transport.message.Response.YrpcResponse;
import com.ydlclass.YrpcBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * 服务调用方的handler处理器
 * @Author duWenJian
 * @Date 2024/2/20 12:57
 * @Version 1.0
 */
@Slf4j
public class MyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        YrpcResponse yrpcResponse = (YrpcResponse) msg;
        String returnValue = (String)yrpcResponse.getResponseBody();

        // todo 需要针对响应码进行处理

        returnValue = returnValue == null ? "HeartBeat" : returnValue;

        log.debug("从server发送给Client的响应：{}",returnValue);
        YrpcBootstrap.PENDING_REQUEST.get(yrpcResponse.getRequestId()).complete(returnValue);
        if (log.isDebugEnabled()) {
            log.debug("已寻找到编号为:{}的CompletableFuture,处理响应结果",yrpcResponse.getRequestId());
        }
    }
}
